const Koa = require('koa');
const route = require('koa-route');
const path = require('path');
const serve = require('koa-static');
const mongoose = require('mongoose');
const resitDatabase = require('./resitDatabase');
const website = require('./website');
const api = require('./api/api');
const app = new Koa();

/* 初始化 */
// 连接数据库
resitDatabase.connect();

// 注册全部 api
api.registerAll(app);

app.use(async ctx => {
    /*
    ctx.url  /v1/resit/234160534?limt=1
    ctx.originalUrl  /v1/resit/234160534?limt=1
    ctx.origin  http://api.localhost:1338
    ctx.href  http://api.localhost:1338/v1/resit/234160534?limt=1
    ctx.path  /v1/resit/234160534
    ctx.host  api.localhost:1338
    ctx.hostname  api.localhost
    ctx.fresh  false
     */
    //console.log(`ctx.url  ${ctx.url}`);
    //console.log(`ctx.originalUrl  ${ctx.originalUrl}`);
    //console.log(`ctx.origin  ${ctx.origin}`);
    //console.log(`ctx.href  ${ctx.href}`);
    //console.log(`ctx.path  ${ctx.path}`);
    //console.log(`ctx.host  ${ctx.host}`);
    //console.log(`ctx.hostname  ${ctx.hostname}`);
    //console.log(`ctx.fresh  ${ctx.fresh}`);
    ctx.body = '到底了';
});


/* 静态资源处理 */
app.use(serve(path.join(__dirname, 'static')));
/* 监听服务器端口 */
app.listen(website.url.port, () => {
    console.log(`${website.url.join()} 服务器已经启动！`);
});